package clock
{
   import flash.events.*;
   import flash.display.*;
   import flash.text.*;
   import flash.geom.*;
   import flash.utils.*;
   import flash.net.*;
   import flash.ui.*;
   // as3core
   import com.adobe.utils.*;
   // greensock
   //import com.greensock.*;
   //import com.greensock.easing.*;

   /**
    * @author   cjboy1984
    */
   public class mcClock extends MovieClip
   {
      //fla
      public var tfDate:TextField;

      // date
      private const NEW_DELAY:Number = 60000;
      private var timer:Timer;

      /* constructor */
      public function mcClock()
      {
         stop();

         addEventListener(Event.ADDED_TO_STAGE, onAdd);
         addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
      }

      // ------------------ LINE -------------------

      /* original x/y position */
      public function get orgX():Number { return sw / 2; }
      public function get orgY():Number { return sh / 2; }

      // ################ Protected ################

      // ################# Private #################

      private function onAdd(e:Event):void
      {
         // Use 1ms at first but it will adjust the delay to NEW_DELAY when date.milliseconds <= 100
         timer = new Timer(1);
         timer.addEventListener(TimerEvent.TIMER, onTimer);
         timer.start();
      }

      private function onRemove(e:Event):void
      {
         timer.stop();
         timer.removeEventListener(TimerEvent.TIMER, onTimer);

         // return focus to stage
         stage.focus = null;
      }

      // ------------------ LINE -------------------

      private function onTimer(e:TimerEvent = null):void
      {
         var date:Date = new Date();

         // Wait for 1st sync
         if (date.milliseconds <= 100 && timer.delay != NEW_DELAY)
         {
            timer.delay = NEW_DELAY;
         }

         if(timer.delay == NEW_DELAY)
         {
            tfDate.text = NumberFormatter.addLeadingZero(date.hours) + ':' + NumberFormatter.addLeadingZero(date.minutes);
         }
      }

      // ------------------ LINE -------------------

      /* get stage.stageWidth/Height */
      private function get sw():Number { return stage.stageWidth; }
      private function get sh():Number { return stage.stageHeight; }

      // ------------------ LINE -------------------

   }

}